﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms
{
    public partial class WorkStatisticsForm : UIPage
    {
        // 数据模型
        private List<WorkData> summaryData = new List<WorkData>();
        
        // 图表控件 - 只保留需要的控件
        private UIBarChart summaryBarChart;
        private UILineChart workLineChart;

        public WorkStatisticsForm()
        {
            InitializeComponent();// 调用默认的初始化方法
            // 注册初始化事件
            this.Initialize += WorkStatisticsForm_Initialize;
        }

        private void WorkStatisticsForm_Initialize(object sender, EventArgs e)
        {
            // 加载数据
            LoadData();

            // 创建主布局
            CreateMainLayout();
        }

        private void LoadData()
        {
            // 示例数据 - 汇总数据
            summaryData.Clear();
            summaryData.Add(new WorkData { Name = "清洗", Count = 120 });
            summaryData.Add(new WorkData { Name = "消毒", Count = 107 });
            summaryData.Add(new WorkData { Name = "灭菌", Count = 98 });
            summaryData.Add(new WorkData { Name = "打包", Count = 85 });
        }

        private void CreateMainLayout()
        {
            // 清除现有控件
            this.Controls.Clear();

            // 设置页面标题
            this.Text = "工作统计";

            // 创建主TableLayoutPanel
            TableLayoutPanel mainLayout = new TableLayoutPanel();
            mainLayout.Dock = DockStyle.Fill;
            mainLayout.ColumnCount = 2;
            mainLayout.RowCount = 2;
            mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
            mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
            mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 0F)); // 第二行高度为0，实际不显示
            mainLayout.Padding = new Padding(10);
            this.Controls.Add(mainLayout);

            // 创建工作趋势折线图面板 - 放在第一行第一列
            UITitlePanel workLinePanel = new UITitlePanel();
            workLinePanel.Text = "工作趋势";
            workLinePanel.Dock = DockStyle.Fill;
            workLinePanel.Margin = new Padding(5);
            mainLayout.Controls.Add(workLinePanel, 0, 0);

            // 创建折线图
            workLineChart = new UILineChart();
            workLineChart.Dock = DockStyle.Fill;
            workLineChart.FillColor = Color.White;
            workLineChart.Font = new Font("微软雅黑", 12F);
            workLineChart.LegendFont = new Font("微软雅黑", 9F);
            workLineChart.SubFont = new Font("微软雅黑", 9F);
            workLinePanel.Controls.Add(workLineChart);
            InitWorkLineChart();

            // 创建汇总数据面板 - 放在第一行第二列
            UITitlePanel summaryPanel = new UITitlePanel();
            summaryPanel.Text = "汇总数据";
            summaryPanel.Dock = DockStyle.Fill;
            summaryPanel.Margin = new Padding(5);
            mainLayout.Controls.Add(summaryPanel, 1, 0);

            // 创建汇总数据图表
            summaryBarChart = new UIBarChart();
            summaryBarChart.Dock = DockStyle.Fill;
            summaryBarChart.FillColor = Color.White;
            summaryBarChart.Font = new Font("微软雅黑", 12F);
            summaryBarChart.LegendFont = new Font("微软雅黑", 9F);
            summaryBarChart.SubFont = new Font("微软雅黑", 9F);
            summaryPanel.Controls.Add(summaryBarChart);
            InitSummaryChart();
        }

        // 折线图初始化方法
        private void InitWorkLineChart()
        {
            // 确保控件存在
            if (workLineChart == null) return;
            
            // 清除现有数据
            workLineChart.Option.Series.Clear();
            workLineChart.Option.XAxis.Data.Clear();
            
            // 设置标题
            workLineChart.Option.Title = new UITitle();
            workLineChart.Option.Title.Text = "工作趋势";
            workLineChart.Option.Title.SubText = "各类工作完成情况";
            
            // 设置图表内边距
            if (workLineChart.Option.Grid == null)
              //  workLineChart.Option.Grid = new UIGrid();
            workLineChart.Option.Grid.Left = 60;
            workLineChart.Option.Grid.Bottom = 40;
            
            // 设置图例
            workLineChart.Option.Legend = new UILegend();
            workLineChart.Option.Legend.Orient = UIOrient.Horizontal;
            workLineChart.Option.Legend.Top = UITopAlignment.Top;
            workLineChart.Option.Legend.Left = UILeftAlignment.Left;
            
            // 添加图例数据
            workLineChart.Option.Legend.AddData("清洗");
            workLineChart.Option.Legend.AddData("消毒");
            workLineChart.Option.Legend.AddData("灭菌");
            workLineChart.Option.Legend.AddData("打包");
            
            // 设置工具提示
            workLineChart.Option.ToolTip.Visible = true;
            
            // 添加X轴数据（时间）
            for (int i = 8; i <= 18; i++)
            {
                workLineChart.Option.XAxis.Data.Add($"{i}:00");
            }
            
            // // 添加清洗数据系列
            // UILineSeries cleaningSeries = new UILineSeries();
            // cleaningSeries.Name = "清洗";
            // cleaningSeries.Symbol = UILineSymbol.EmptyCircle;
            // cleaningSeries.AddData(0);
            // cleaningSeries.AddData(15);
            // cleaningSeries.AddData(30);
            // cleaningSeries.AddData(45);
            // cleaningSeries.AddData(60);
            // cleaningSeries.AddData(75);
            // cleaningSeries.AddData(90);
            // cleaningSeries.AddData(100);
            // cleaningSeries.AddData(110);
            // cleaningSeries.AddData(115);
            // cleaningSeries.AddData(120);
            // workLineChart.Option.Series.Add(cleaningSeries);
            
            // // 添加消毒数据系列
            // UILineSeries disinfectionSeries = new UILineSeries();
            // disinfectionSeries.Name = "消毒";
            // disinfectionSeries.Symbol = UILineSymbol.EmptyCircle;
            // disinfectionSeries.AddData(0);
            // disinfectionSeries.AddData(10);
            // disinfectionSeries.AddData(25);
            // disinfectionSeries.AddData(40);
            // disinfectionSeries.AddData(55);
            // disinfectionSeries.AddData(70);
            // disinfectionSeries.AddData(80);
            // disinfectionSeries.AddData(90);
            // disinfectionSeries.AddData(95);
            // disinfectionSeries.AddData(100);
            // disinfectionSeries.AddData(107);
            // workLineChart.Option.Series.Add(disinfectionSeries);
            
            // // 添加灭菌数据系列
            // UILineSeries sterilizationSeries = new UILineSeries();
            // sterilizationSeries.Name = "灭菌";
            // sterilizationSeries.Symbol = UILineSymbol.EmptyCircle;
            // sterilizationSeries.AddData(0);
            // sterilizationSeries.AddData(8);
            // sterilizationSeries.AddData(20);
            // sterilizationSeries.AddData(35);
            // sterilizationSeries.AddData(50);
            // sterilizationSeries.AddData(65);
            // sterilizationSeries.AddData(75);
            // sterilizationSeries.AddData(85);
            // sterilizationSeries.AddData(90);
            // sterilizationSeries.AddData(95);
            // sterilizationSeries.AddData(98);
            // workLineChart.Option.Series.Add(sterilizationSeries);
            
            // // 添加打包数据系列
            // UILineSeries packagingSeries = new UILineSeries();
            // packagingSeries.Name = "打包";
            // packagingSeries.Symbol = UILineSymbol.EmptyCircle;
            // packagingSeries.AddData(0);
            // packagingSeries.AddData(5);
            // packagingSeries.AddData(15);
            // packagingSeries.AddData(30);
            // packagingSeries.AddData(45);
            // packagingSeries.AddData(55);
            // packagingSeries.AddData(65);
            // packagingSeries.AddData(70);
            // packagingSeries.AddData(75);
            // packagingSeries.AddData(80);
            // packagingSeries.AddData(85);
            // workLineChart.Option.Series.Add(packagingSeries);
            
            // 刷新图表
            workLineChart.Refresh();
        }

        private void InitSummaryChart()
        {
            // 确保控件存在
            if (summaryBarChart == null) return;
            // 清除现有数据
            summaryBarChart.Option.Series.Clear();
            summaryBarChart.Option.XAxis.Data.Clear();

            // 设置标题
            summaryBarChart.Option.Title = new UITitle();
            summaryBarChart.Option.Title.Text = "工作汇总";
            summaryBarChart.Option.Title.SubText = "今日完成情况";

            // 设置X轴标签
            summaryBarChart.Option.XAxis.AxisLabel.Angle = 0;

            // 设置图表内边距
            if (summaryBarChart.Option.Grid == null)
             //   summaryBarChart.Option.Grid = new UIGrid();
            summaryBarChart.Option.Grid.Left = 60;
            summaryBarChart.Option.Grid.Bottom = 40;

            // 设置图例
            summaryBarChart.Option.Legend = new UILegend();
            summaryBarChart.Option.Legend.Orient = UIOrient.Horizontal;
            summaryBarChart.Option.Legend.Top = UITopAlignment.Top;
            summaryBarChart.Option.Legend.Left = UILeftAlignment.Left;
            summaryBarChart.Option.Legend.AddData("完成数量");

            // 设置工具提示
            summaryBarChart.Option.ToolTip = new UIBarToolTip();

            // 添加X轴数据
            foreach (var item in summaryData)
                summaryBarChart.Option.XAxis.Data.Add(item.Name);

            // 添加数据系列
            UIBarSeries summarySeries = new UIBarSeries();
            summarySeries.Name = "完成数量";
            foreach (var item in summaryData)
            {
                summarySeries.AddData(item.Count);
            }

            summaryBarChart.Option.Series.Add(summarySeries);

            // 刷新图表
            summaryBarChart.Refresh();
        }

        private void WorkStatisticsForm_Load(object sender, EventArgs e)
        {
            // 可以添加加载事件处理
        }
    }

    // 数据模型类
    public class WorkData
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }
}
